package.cpath = "luaclib/?.so"
package.path = "lualib/?.lua;shao/?.lua"

local socket = require "clientsocket"
local packdeal = require "packdeal_cli"
local send_package = packdeal.send_package
local unpack_json = packdeal.unpack_package

local function unpack_package(text)
	local size = #text
	if size < 2 then
		return nil, text
	end
	local s = text:byte(1) * 256 + text:byte(2)
	if size < s+2 then
		return nil, text
	end

	return text:sub(3,2+s), text:sub(3+s)
end

local function recv_package(last)
	local result
	result, last = unpack_package(last)
	if result then
		return result, last
	end
	local r = socket.recv(fd)
	if not r then
		return nil, last
	end
	if r == "" then
		error "Server closed"
	end
	return unpack_package(last .. r)
end


local function dispatch_package()
	while true do
		local v
		v, last = recv_package(last)
		if not v then
			break
		end
        print("RESPONSE: " .. v)
        local json_data = unpack_json(v)
	end
end

local fd = assert(socket.connect("127.0.0.1", 8890))
send_package( fd, { cmd = 11, account='test_1001', password='123456' } )     -- login
for i=1,10000000 do end
send_package( fd, { cmd = 120, game_type = 2,  hu_type = 1, total_ju = 2, room_id = 888888} )     -- create room
for i=1,10000000 do end
send_package( fd, { cmd = 356} )     -- ready
for i=1,10000000 do end

local fd2 = assert(socket.connect("127.0.0.1", 8890))
send_package( fd2, { cmd = 11, account='test_1002', password='123456' } )     -- login
for i=1,10000000 do end
send_package( fd2, { cmd = 106, room_id = 888888} )     -- create room
for i=1,10000000 do end
send_package( fd2, { cmd = 356} )     -- ready

local fd3 = assert(socket.connect("127.0.0.1", 8890))
send_package( fd3, { cmd = 11, account='test_1003', password='123456' } )     -- login
for i=1,10000000 do end
send_package( fd3, { cmd = 106, room_id = 888888} )     -- create room
for i=1,10000000 do end
send_package( fd3, { cmd = 356} )     -- ready

local fd4 = assert(socket.connect("127.0.0.1", 8890))
send_package( fd4, { cmd = 11, account='test_1004', password='123456' } )     -- login
for i=1,10000000 do end
send_package( fd4, { cmd = 106, room_id = 888888} )     -- create room
for i=1,10000000 do end
send_package( fd4, { cmd = 356} )     -- ready


for i=1,1000000000000 do
   --dispatch_package()
end
